<?php

/**
 * XFramework
 *
 * @author Diogo Dias Barreiros outhom
 * @contact diogo.barreiros@gmail.com
 * 
 * 134680
 */
class AccountController extends AbstractController {

    private static $showed = false;
    
    public function onLoad() {
        if (!self::$showed) {
            self::$showed = true;
            $this->lateralBar();
        }
    }

    public static function Action() {
        URLManager::setPath('account', 'manager');
        URLManager::redirect();
    }

    public static function loginAction() {
        if (AccountsModel::isLogged()) {
            URLManager::setPath('account', 'manager');
            URLManager::redirect();
        } else {
            if (self::login()) {
                URLManager::setPath('account', 'manager');
                URLManager::redirect();
            } else {
                SiteTitle::makeTitle(array('Account', 'Login'));
                LayoutManager::setView('content', 'account', 'login');
            }
        }
    }

    private function login() {
        if (isset($_POST['name'])) {
            if (empty($_POST['name']) || empty($_POST['password'])) {
                LayoutManager::setVar('error-login', 'Fulfill all fields.', 'error');
            } else {
                $AccountsModel = new AccountsModel();
                $AccountsModel->fromArray($_POST);
                if ($AccountsModel->login()) {
                    $AccountsModel->makeLogged();
                    return true;
                } else {
                    LayoutManager::setVar('error-login', 'Incorrect name or password.', 'error');
                }
            }
        }
        return false;
    }

    public function managerAction() {
        if (!AccountsModel::isLogged()) {
            URLManager::setPath('account', 'login');
            URLManager::redirect();
        }
        SiteTitle::makeTitle(array('Account', 'Manager'));
        LayoutManager::setView('main', 'account', 'manager');
    }

    public function logoutAction($params) {
        AccountsModel::logout();
        $controller = null;
        $action = null;
        $params2 = array();
        if (!empty($params[0])) {
            $controller = $params[0];
            unset($params[0]);
            if (!empty($params[1])) {
                $action = $params[1];
                unset($params[1]);
                if (!empty($params[2])) {
                    $params2 = array_values($params);
                }
            }
        } else {
            $controller = 'account';
            $action = 'login';
        }
        URLManager::setPath($controller, $action, $params2);
        URLManager::redirect();
    }

    public function lateralBar() {
        $logged = AccountModel::isLogged();

        if (!$logged) {
            if ($this->login()) {
                URL::reload();
            } else {
                $this->Layout->set('logged', false);
            }
        } else {
            Loader::loadModel('Player');

            $this->Model->fromArray($_SESSION['account']);

            $chars = PlayersModel::getByAccount($this->Model);

            if (isset($chars[0])) {
                LayoutManager::setRepeatID('characters');
                foreach ($chars as $char) {
                    URLManager::setPath('character', 'view', array($char->getName()));
                    LayoutManager::setRepeatVar('link', URLManager::getURI());
                    LayoutManager::setRepeatVar('charlevel', $char->getLevel());
                    LayoutManager::setRepeatVar('charname', $char->getName());
                }
                LayoutManager::parseView(array('account', 'lateralbar'));
            }
        }
        echo '</div>';
    }

    public static function showBar() {
        $logged = AccountModel::isLogged();
        if (self::login()) {
            URLManager::reload();
        }
        echo '<div id="accountBar">';
        if (!$logged) {
            Layout::parseView(array('account', 'login'));
        } else {
            Loader::loadModel('Players');

            $AccountsModel = new AccountModel();
            $AccountsModel->fromArray($_SESSION['account']);

            $chars = PlayersModel::getByAccount($AccountsModel);

            if (isset($chars[0])) {
                LayoutManager::setRepeatID('characters');
                foreach ($chars as $char) {
                    URLManager::setPath('character', 'view', array($char->getName()));
                    LayoutManager::setRepeatVar('link', URLManager::getURI());
                    LayoutManager::setRepeatVar('charlevel', $char->getLevel());
                    LayoutManager::setRepeatVar('charname', $char->getName());
                }
                LayoutManager::parseView(array('account', 'lateralbar'));
            }
        }
        echo '</div>';
    }

    public static function showMenuData() {
        if (AccountsModel::isLogged()) {
            $AccountsModel = new AccountsModel();
            $AccountsModel->fromArray($_SESSION['account']);
            URLManager::setPath('account', 'logout');
            echo '<li class="right"><a href="' . URLManager::getURI() . URLManager::getRequestURI(true) . '">Log out</a></li>';
        }
    }

}

?>
